#include "CUDAAcceleration.h"



__device__ float downsampleFourPoints(float a, float b, float c, float d){
	float downsampled = 0.0;
	int hit = 0;
		
	hit += a > 10.0 ? 1 : 0;
	hit += b > 10.0 ? 1 : 0;
	hit += c > 10.0 ? 1 : 0;
	hit += d > 10.0 ? 1 : 0;


	downsampled = a + b + c + d;
	downsampled = hit == 4 ? downsampled / 4.0 : 0.0;

	return downsampled;
}



__global__ void downsampleGPU(float* in, float* out0, int width, int height){
	
	int x = blockIdx.x * blockDim.x + threadIdx.x;
	int y = blockIdx.y * blockDim.y + threadIdx.y;

	__shared__ float sampled[32][20];

	sampled[threadIdx.x][threadIdx.y] = in[x + y * width];
	__syncthreads();

	if(threadIdx.x % 2 == 0 && threadIdx.y % 2 == 0){

		out0[(x/2) + (y/2) * width/2] = downsampleFourPoints(
			sampled[threadIdx.x][threadIdx.y],
			sampled[threadIdx.x + 1][threadIdx.y],
			sampled[threadIdx.x][threadIdx.y + 1],
			sampled[threadIdx.x + 1][threadIdx.y + 1]);
	}
}


void launchDownsampleGPU(float* in, float* out0, int width, int height){
	dim3 blockSize(32, 20);
	dim3 gridSize(20, 24);
	downsampleGPU<<<gridSize, blockSize>>>(in, out0, width, height);
}









